@Entity()
export class Article {
@PrimaryGeneratedColumn()
IdArticle: number | undefined
@ManyToOne(() => User, (user) => user.Articles)
user: User | undefined
@Column()
Title: string | undefined
@Column()
ArticleType: string | undefined
@Column()
Position: string | undefined
@Column()
Description: string | undefined
@Column()
PlayedTime: Date | undefined
@CreateDateColumn()
CreatedTime: Date | undefined
}
async findArticle(idArticle: number) {
return await this.dataSource.manager
.findBy(Article, { IdArticle: idArticle });
}
async saveArticle(article: any) {
return await this.dataSource.manager.save(Article, article);
}
async deleteArticle(idArticle: number) {
return await this.dataSource.getRepository(Article)
.delete({IdArticle: idArticle});
}
ArticleController.get('/Article/getArticle/:IdArticle', (req, res) => {
if (req.params == null)
{
res.status(400).send({status:"body error"});
return;
}
let IdArticle = parseInt(req.params["IdArticle"]) ? parseInt(req.params["IdArticle"]) : 0;
articleDao.findArticle(IdArticle).then((articles: Array<Article>) => {
if (articles.length > 0)
{
let article:Article = articles[0];
res.status(200).send(article);
}
else
{
res.status(400).send({status:"article not found"});
}
});
});
ArticleController.post('/Article/saveArticle', (req, res) => {
if (req.body == null)
{
res.status(400).send({status:"body error"});
return;
}
articleDao.saveArticle(req.body).then((data: any) => {
res.status(200).send({status:"article save OK"});
});
});
ArticleController.delete('/Article/deleteArticle/:IdArticle', (req, res) => {
let idArticle = parseInt(req.params["IdArticle"]) ? parseInt(req.params["IdArticle"]) : 0;
articleDao.deleteArticle(idArticle).then((data: any) => {
res.status(200).send({status:"article delete OK"});
});
});
本日完成基本底層資料存取實作,後續針對資料連結部分進行調整和圖檔上傳部分進行開發.